package com.sskj.jgxt.impl;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.sskj.core.exception.MException;
import com.sskj.jgxt.RestParamVO;
import com.sskj.jgxt.RestQueryVO;
import com.sskj.jgxt.eng.domain.entity.DingJianDuEng;
import com.sskj.jgxt.jd.vo.TianQiVO;
import com.sskj.jgxt.param.EngQueryParam;
import com.sskj.utils.CalendarUtil;
import com.sskj.utils.HttpUtils;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import springfox.documentation.annotations.ApiIgnore;

@RestController
@RequestMapping(value = "/wechat/tq", method = { RequestMethod.POST, RequestMethod.GET })
@Api(value = "天气信息", description = "天气信息")
public class WeChartTqServiceImpl {

	protected final Log logger = LogFactory.getLog(WeChartTqServiceImpl.class);

	@Value("${sskj.conf.restUrl}")
	private String restUrl;

	@CrossOrigin(origins = "*", maxAge = 3600)
	@RequestMapping(value = "/page", method = RequestMethod.GET)
	@ApiOperation(value = "查询工程", httpMethod = "GET")
	@ApiImplicitParams({ @ApiImplicitParam(name = "keyword", value = "关键字", paramType = "form", dataType = "string"),
			@ApiImplicitParam(name = "jdfzr", value = "监督组成员", paramType = "form", dataType = "string"),
			@ApiImplicitParam(name = "pageNumber", value = "页号", paramType = "form", dataType = "int"),
			@ApiImplicitParam(name = "pageSize", value = "页的大小", paramType = "form", dataType = "int") })
	public List<DingJianDuEng> queryPage(@ApiIgnore EngQueryParam engQueryParam, @ApiIgnore Pageable pageable)
			throws MException {

		List<DingJianDuEng> result = new ArrayList<DingJianDuEng>();

		RestQueryVO data = new RestQueryVO();

		data.setPage(pageable.getPageNumber() - 1);
		data.setPageSize(pageable.getPageSize());
		data.setNameQueryName("dd.jiandueng");

		RestParamVO param = new RestParamVO();
		param.setPn("engName");
		param.setValue(engQueryParam.getKeyword());
		data.getParams().add(param);

		RestParamVO param2 = new RestParamVO();
		param2.setPn("jdfzr");
		param2.setValue(engQueryParam.getJdfzr());

		data.getParams().add(param2);

		try {
			JSONObject obj = HttpUtils.httpPost(restUrl + "engcontrol/queryJDEngListPage", data);
			if (obj != null) {
				result = JSON.parseArray(obj.getString("resultls"), DingJianDuEng.class);
			}
		} catch (Exception e) {
			e.printStackTrace();
			throw new MException("request_timeout");
		}

		return result;

	}

	@CrossOrigin(origins = "*", maxAge = 3600)
	@RequestMapping(value = "/get", method = { RequestMethod.POST })
	@ApiImplicitParams({ @ApiImplicitParam(name = "spId", value = "记录ID", paramType = "form", dataType = "string") })
	@ApiOperation(value = "获取安全日志记录", notes = "获取安全日志记录", httpMethod = "GET")
	public TianQiVO get(String spId) throws Exception {

		TianQiVO jl = new TianQiVO();

		try {
			Map<String, String> querys = new HashMap<String, String>();

			jl = getTQMX(querys);

			Calendar c = Calendar.getInstance();
			Calendar today = CalendarUtil.getToday();
			int hour = c.get(Calendar.HOUR_OF_DAY);
			if (hour >= 10) {

				jl.setJlrq(CalendarUtil.convertToString(today));
			} else {
				today.add(Calendar.DAY_OF_MONTH, -1);
				jl.setJlrq(CalendarUtil.convertToString(today));
			}

		} catch (Exception e) {
			e.printStackTrace();
			throw new MException("request_timeout");
		}
		return jl;
	}

	public TianQiVO getTQMX(Map<String, String> querys) {

		String path = "tqcontrol/getTq";
		String method = "GET";
		Map<String, String> headers = new HashMap<String, String>();
		try {
			HttpResponse response = HttpUtils.doGet(restUrl, path, method, headers, querys);

			if (response.getStatusLine().getStatusCode() != 200) {
				System.out.println("request url failed, http code=" + response.getStatusLine().getStatusCode()
						+ ", url=" + restUrl);
				return null;
			}
			HttpEntity entity = response.getEntity();
			if (entity != null) {
				String resultStr = EntityUtils.toString(entity, "utf-8");

				return JSONObject.parseObject(resultStr, TianQiVO.class);

			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		return null;
	}

}
